//
//  ThemeTabBarItem.m
//  kabu
//
//  Created by XiangDongMBP on 2017/4/12.
//  Copyright © 2017年 East. All rights reserved.
//

#import "ThemeTabBarItem.h"
#import "ThemeManager.h"

@implementation ThemeTabBarItem
#pragma mark - life cycle
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ThemeManagerThemeDidChangedNotification object:nil];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangedNotification:) name:ThemeManagerThemeDidChangedNotification object:nil];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    self = [self init];
    if (self) {
        self.title = title;
        self.imageName = imageName;
        self.selectedImageName = selectedImageName;
    }
    return self;
}

#pragma mark - event methods
// 主题改变之后重新加载图片
- (void)themeChangedNotification:(NSNotification *)notification {
    [self reloadThemeImage];
}

#pragma mark - private methods
- (void)reloadThemeImage {
    ThemeManager * themeManager = [ThemeManager sharedManager];
    
    if (self.imageName != nil) {
        UIImage * image = [themeManager themeImageWithName:self.imageName];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self setImage:image];
    }
    
    if (self.selectedImageName != nil) {
        UIImage * selectedImage = [themeManager themeImageWithName:self.selectedImageName];
        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self setSelectedImage:selectedImage];
    }
}

#pragma mark - getters and setters
- (void)setImageName:(NSString *)imageName {
    if (![_imageName isEqualToString:imageName]) {
        _imageName = [imageName copy];
    }
    
    [self reloadThemeImage];
}

- (void)setSelectedImageName:(NSString *)selectedImageName {
    if (![_selectedImageName isEqualToString:selectedImageName]) {
        _selectedImageName = [selectedImageName copy];
    }
    
    [self reloadThemeImage];
}

@end
